<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const fn = (arr, n) => {
        return new Promise((resolve) => {
          let resultArr = [];
          let downCount = 0;
          let index = 0;

          const request = async () => {
            if (index < arr.length) {
              // 记录当前索引
              let i = index;
              let url = arr[index];
              index++;
              try {
                const res = await axiosApi(url);
                downCount++;
                resultArr[i] = res;
              } catch (error) {
                downCount++;
                resultArr[i] = error;
              } finally {
                //   全部发送完成
                if (downCount === arr.length) {
                  resolve(resultArr);
                }
                // 一个请求完成之后开始下一个请求
                request();
              }
            }
          };
          let times = Math.max(n, arr.length);
          for (let i = 0; i < times; i++) {
            request();
          }
        });
      };
    </script>
  </body>
</html>
